package com.miyu.system.controller.sys;

import cn.hutool.captcha.*;

import com.alibaba.fastjson2.JSONObject;
import com.miyu.common.utils.commonUtils.IpUtils;
import com.miyu.system.service.sys.UserLoginService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户处理类 登录 退出 处理
 *
 * @author longm
 * @date 2022/10/08
 */
@RestController
@RequestMapping("/sys-user")
public class UserLogin {

    @Resource
    private UserLoginService loginService;

    /**
     * 获取登录验证码
     *
     * @param request  请求
     * @param response 响应
     * @return {@link HttpServletResponse}
     * @throws Exception 异常
     */
    @RequestMapping("checkImg")
    public HttpServletResponse checkImg(HttpServletRequest request, HttpServletResponse response) throws Exception{
        GifCaptcha gifCaptcha = CaptchaUtil.createGifCaptcha(200, 100, 4);
        /*缓存验证码 5分钟过期*/
        loginService.checkImg(IpUtils.getIpAddr(),gifCaptcha.getCode());

        gifCaptcha.write(response.getOutputStream());
        return null;
    }

    /**
     * 用户登录
     *
     * @param user 用户
     * @return {@link JSONObject}
     */
    @RequestMapping(value = "login")
    public JSONObject userLogin(@RequestBody  JSONObject user) {
        return loginService.userLogin(user);
    }

    /**
     * 用户退出
     *
     * @return {@link JSONObject}
     */
    @RequestMapping(value = "userLoginOut")
    public JSONObject userLoginOut() {
        return loginService.userLoginOut();
    }
}
